/*******************************************************************************
 * Copyright (c) 2006, 2020 Mountainminds GmbH & Co. KG and Contributors
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *
 ******************************************************************************/
package org.eclipse.eclemma.internal.core.analysis;

import static org.junit.Assert.assertEquals;

import java.io.IOException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import org.eclipse.eclemma.core.JavaProjectKit;

/**
 * Tests for {@link SessionAnalyzer}.
 */
public class SessionAnalyzerTest {

  private JavaProjectKit javaProject;
  private SessionAnalyzer sessionAnalyzer;

  @Before
  public void setup() throws Exception {
    javaProject = new JavaProjectKit();
    sessionAnalyzer = new SessionAnalyzer();
  }

  @After
  public void teardown() throws Exception {
    javaProject.destroy();
  }

  @Test
  public void testGetNameSourceFolder() throws CoreException {
    IPackageFragmentRoot source = javaProject.createSourceFolder("src");
    assertEquals("src", sessionAnalyzer.getName(source));
  }

  @Test
  public void testGetNameNestedSourceFolder() throws CoreException {
    javaProject.project.getFolder("src").create(false, true, null);
    javaProject.project.getFolder("src/main").create(false, true, null);
    IPackageFragmentRoot source = javaProject
        .createSourceFolder("src/main/java");
    assertEquals("src/main/java", sessionAnalyzer.getName(source));
  }

  @Test
  public void testGetNameProjectRootSourceFolder() throws CoreException {
    IPackageFragmentRoot source = javaProject.javaProject
        .getPackageFragmentRoot(javaProject.project);
    javaProject.addClassPathEntry(JavaCore.newSourceEntry(source.getPath()));

    assertEquals("UnitTestProject", sessionAnalyzer.getName(source));
  }

  @Test
  public void testGetNameLocalLibrary() throws CoreException, IOException {
    IPackageFragmentRoot library = javaProject.createJAR(
        "testdata/bin/signatureresolver.jar", "/sample.jar", null, null);

    assertEquals("sample.jar", sessionAnalyzer.getName(library));
  }

  @Test
  public void testGetNameLocalLibraryInFolder()
      throws CoreException, IOException {
    javaProject.project.getFolder("libs").create(false, true, null);
    javaProject.project.getFolder("libs/runtime").create(false, true, null);
    IPackageFragmentRoot library = javaProject.createJAR(
        "testdata/bin/signatureresolver.jar", "/libs/runtime/sample.jar", null,
        null);

    assertEquals("libs/runtime/sample.jar", sessionAnalyzer.getName(library));
  }

  @Test
  public void testGetNameExternalLibrary() throws CoreException, IOException {
    IPackageFragmentRoot library = javaProject
        .createExternalJAR("testdata/bin/signatureresolver.jar", null, null);

    assertEquals(library.getElementName(), sessionAnalyzer.getName(library));
  }

}
